package com.wit.dao.impl;

import java.util.ArrayList;
import java.util.List;

import hibernate.HibernateSessionFactory;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.DetachedCriteria;

import com.wit.dao.SeparationDao;
import com.wit.po.Department;
import com.wit.po.Employee;
import com.wit.po.Separation;

public class SeparationDaoImpl implements SeparationDao {

	
	public boolean addSeparation(Separation separation) {
		Session s = null;
		Transaction tx = null;
		boolean isFinished = false;
		try{
			s = HibernateSessionFactory.getSession();
			tx =  s.beginTransaction();
			s.save(separation);
			tx.commit();
			s.close();
			isFinished = true;
		}catch(HibernateException e){
			tx.rollback();
			e.printStackTrace();
		}finally{
			if(s.isOpen())
				s.close();
		}
		return isFinished;
	}

	
	public boolean deleteSeparation(Separation separation) {
		Session s = null;
		Transaction tx = null;
		boolean isFinished = false;
		try{
			s = HibernateSessionFactory.getSession();
			tx =  s.beginTransaction();
			s.delete(separation);
			tx.commit();
			s.close();
			isFinished = true;
		}catch(HibernateException e){
			tx.rollback();
			e.printStackTrace();
		}finally{
			if(s.isOpen())
				s.close();
		}
		return isFinished;
	}

	
	public List<Separation> findSeparationByCondition(DetachedCriteria dc,
			Integer startRecord, Integer pageSize) {
		List<Separation> compList = new ArrayList<Separation>();
		try{
			compList = (List<Separation>)dc.getExecutableCriteria(HibernateSessionFactory.getSession()).setFirstResult(startRecord).setMaxResults(pageSize).list();
		}catch(HibernateException e){
			e.printStackTrace();
		}
		return compList;
	}

	
	public Separation findSeparationById(String SeparationId) {
		Session s = null;
		Separation separation = null;
		try{
			s = HibernateSessionFactory.getSession();
			List<Separation> compList = (List<Separation>)s.createQuery("FROM Separation WHERE separationId = ?").setString(0, SeparationId).list();
			if(compList.size() == 1)
				separation = compList.get(0);
			s.close();
		}catch(HibernateException e){
			e.printStackTrace();
		}finally{
			if(s.isOpen())
				s.close();
		}
		return separation;
	}

	
	public boolean modifySeparation(Separation separation) {
		Session s = null;
		Transaction tx = null;
		boolean isFinished = false;
		try{
			s = HibernateSessionFactory.getSession();
			tx =  s.beginTransaction();
			s.update(separation);
			tx.commit();
			s.close();
			isFinished = true;
		}catch(HibernateException e){
			tx.rollback();
			e.printStackTrace();
		}finally{
			if(s.isOpen())
				s.close();
		}
		return isFinished;
	}

	
	public Integer findSeparationCount(DetachedCriteria dcCount) {
		List<Integer> countList = new ArrayList<Integer>();
		try{
			countList = (List<Integer>)dcCount.getExecutableCriteria(HibernateSessionFactory.getSession()).list();
		}catch(HibernateException e){
			e.printStackTrace();
		}
		return countList.get(0);
	}

	
	public List<Department> findAllDepartment() {
		Session s = null;
		List<Department> compList = null;
		try{
			s = HibernateSessionFactory.getSession();
			compList = (List<Department>)s.createQuery("FROM Department").list();
			s.close();
		}catch(HibernateException e){
			e.printStackTrace();
		}finally{
			if(s.isOpen())
				s.close();
		}
		return compList;
	}

	
	public List<Employee> findEmployeeByDepartmentId(String departmentId) {
		Session s = null;
		List<Employee> compList = null;
		try{
			s = HibernateSessionFactory.getSession();
			compList = (List<Employee>)s.createQuery("FROM Employee e WHERE e.department.departmentId=?").setString(0, departmentId).list();
			s.close();
		}catch(HibernateException e){
			e.printStackTrace();
		}finally{
			if(s.isOpen())
				s.close();
		}
		return compList;
	}
	
//	public static void main(String[] args){
//		SeparationDaoImpl sdi = new SeparationDaoImpl();
//		List<Employee> empList = sdi.findEmployeeByDepartmentId("RHLCW001");
//		System.out.println(empList.size());
//	}

}
